# 校验字段类型是否正确
import datetime
import decimal


def caseColumnsType(parameter,columns):
    """

    :param parameter: 参数
    :param columns: 字段列表
    :return:
    """
    parameterKeyList = parameter.keys()
    for c in columns:
        # print("++",c.name, c.type.python_type)
        if c.name in parameterKeyList:
            if c.primary_key == True and c.autoincrement == True:
                continue
            doc = "类型错误"
            if c.doc !=None:
                ",描述:" + c.doc + "类型错误"
            if c.type.python_type == datetime.time and parameter[c.name]!=None:
                # 校验时间
                try:
                    datetime.datetime.strptime(parameter[c.name], "%H:%M:%S")
                except:
                    return None,"参数:" + c.name + doc
                continue

            elif c.type.python_type == datetime.date and parameter[c.name]!=None:
                # 校验日期
                try:
                    datetime.datetime.strptime(parameter[c.name], "%Y-%m-%d")
                except:
                    return None,"参数:" + c.name + doc
                continue
            elif c.type.python_type == datetime.datetime and parameter[c.name]!=None:
                # 校验日期时间
                err_z = False
                try:
                    datetime.datetime.strptime(parameter[c.name], "%Y-%m-%d %H:%M:%S")
                except:
                    err_z = True
                if False==True:
                    try:
                        err_z = False
                        datetime.datetime.strptime(parameter[c.name], "%Y-%m-%d")
                    except:
                        err_z = True
                if err_z!=False:
                    return None,"参数:" + c.name + doc
                continue
            elif c.type.python_type == bool and parameter[c.name] != None:
                # 校验bool
                if parameter[c.name] not in (True,False,0,1):
                    return None,"参数:" + c.name + doc
                continue
            elif c.type.python_type == decimal.Decimal and parameter[c.name] != None:
                # 校验数字
                if type(parameter[c.name]) not in (float,int) :
                    return None, "参数:" + c.name + doc
                continue
            elif c.type.python_type == float and parameter[c.name] != None:
                # 校验浮点数
                if type(parameter[c.name]) not in (float,int) :
                    return None, "参数:" + c.name + doc
                continue
            elif c.type.python_type == int and parameter[c.name] != None:
                # 校验整型
                if type(parameter[c.name]) != int :
                    return None, "参数:" + c.name + doc
                continue
            else:
                pass
    return None,None